﻿<script lang="ts" name="inventory" setup>
import { ref, reactive, onMounted } from "vue";
import { ElMessage } from "element-plus";
import type { FormRules } from "element-plus";
import { formatDate } from '/@/utils/formatTime';
import { Plus } from "@element-plus/icons-vue";
import { UploadRequestOptions } from "element-plus";
import { useInventoryApi } from '/@/api/InboundManagement/inventory';

//父级传递来的函数，用于回调
const emit = defineEmits(["reloadTable"]);
const inventoryApi = useInventoryApi();
const ruleFormRef = ref();

const state = reactive({
	title: '',
	loading: false,
	showDialog: false,
	ruleForm: {} as any,
	stores: {},
	dropdownData: {} as any,
});

// 自行添加其他规则
const rules = ref<FormRules>({
});

// 页面加载时
onMounted(async () => {
  const data = await inventoryApi.getDropdownData(false).then(res => res.data.result) ?? {};
  state.dropdownData.inboundId = data.inboundId ?? [];
});

// 打开弹窗
const openDialog = async (row: any, title: string) => {
	state.title = title;
	row = row ?? {  };
	state.ruleForm = row.id ? await inventoryApi.detail(row.id).then(res => res.data.result) : JSON.parse(JSON.stringify(row));
	state.showDialog = true;
};

// 关闭弹窗
const closeDialog = () => {
	emit("reloadTable");
	state.showDialog = false;
};

// 提交
const submit = async () => {
	ruleFormRef.value.validate(async (isValid: boolean, fields?: any) => {
		if (isValid) {
			let values = state.ruleForm;
			await inventoryApi[state.ruleForm.id ? 'update' : 'add'](values);
			closeDialog();
		} else {
			ElMessage({
				message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
				type: "error",
			});
		}
	});
};

const uploadUpFileHandle = async (options: UploadRequestOptions) => {
	const res = await inventoryApi.uploadUpFile(options);
	state.ruleForm.upFile = res.data.result?.url;
	};

//将属性或者函数暴露给父组件
defineExpose({ openDialog });
</script>
<template>
	<div class="inventory-container">
		<el-dialog v-model="state.showDialog" :width="800" draggable :close-on-click-modal="false">
			<template #header>
				<div style="color: #fff">
					<span>{{ state.title }}</span>
				</div>
			</template>
			<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="auto" :rules="rules">
				<el-row :gutter="35">
					<el-form-item v-show="false">
						<el-input v-model="state.ruleForm.id" />
					</el-form-item>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="盘点单号" prop="inventoryCode">
							<el-input v-model="state.ruleForm.inventoryCode" placeholder="请输入盘点单号" maxlength="32" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="盘点结果" prop="inventoryOutcome">
							<el-input v-model="state.ruleForm.inventoryOutcome" placeholder="请输入盘点结果" maxlength="32" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="仓库名称ID" prop="inboundId">
							<el-select clearable filterable v-model="state.ruleForm.inboundId" placeholder="请选择仓库名称ID">
								<el-option v-for="(item,index) in state.dropdownData.inboundId" :key="index" :value="item.value" :label="item.label" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="盘点类型" prop="inventoryType">
							<el-input v-model="state.ruleForm.inventoryType" placeholder="请输入盘点类型" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="盘点开始时间" prop="inventoryBegin">
							<el-date-picker v-model="state.ruleForm.inventoryBegin" type="date" placeholder="盘点开始时间" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="盘点结束时间" prop="inventoryEnd">
							<el-date-picker v-model="state.ruleForm.inventoryEnd" type="date" placeholder="盘点结束时间" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="入库状态" prop="storageId">
							<el-input v-model="state.ruleForm.storageId" placeholder="请输入入库状态" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="出库状态" prop="outboundId">
							<el-input v-model="state.ruleForm.outboundId" placeholder="请输入出库状态" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="盘点货品" prop="inventoryGoods">
							<el-input v-model="state.ruleForm.inventoryGoods" placeholder="请输入盘点货品" maxlength="32" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="盘点数量" prop="inventoryQuantity">
							<el-input v-model="state.ruleForm.inventoryQuantity" placeholder="请输入盘点数量" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="盘盈数量" prop="inventorySurplus">
							<el-input v-model="state.ruleForm.inventorySurplus" placeholder="请输入盘盈数量" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="盘亏数量" prop="numberLosses">
							<el-input v-model="state.ruleForm.numberLosses" placeholder="请输入盘亏数量" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="盘盈金额" prop="amountProfit">
							<el-input v-model="state.ruleForm.amountProfit" placeholder="请输入盘盈金额" maxlength="32" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="经办人ID" prop="organizationalStructureId">
							<el-input v-model="state.ruleForm.organizationalStructureId" placeholder="请输入经办人ID" maxlength="32" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="备注" prop="remark">
							<el-input v-model="state.ruleForm.remark" placeholder="请输入备注" maxlength="32" type="textarea" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="上传文件" prop="upFile">
							<el-upload
								list-type="picture-card"
								:show-file-list="false"
								:http-request="uploadUpFileHandle">
								<img
									v-if="state.ruleForm.upFile"
									:src="state.ruleForm.upFile"
									@click="state.ruleForm.upFile=''"
									style="width: 100%; height: 100%; object-fit: contain"/>
									<el-icon v-else><Plus /></el-icon>
							</el-upload>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="() => state.showDialog = false">取 消</el-button>
					<el-button @click="submit" type="primary" v-reclick="1000">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>
<style lang="scss" scoped>
:deep(.el-select), :deep(.el-input-number) {
  width: 100%;
}
</style>